package com.keya.config;

import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.migration.JavaMigration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Slf4j
@Component
public class FlywayConfiguration implements CommandLineRunner {
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    private DataSource dataSource;

    @Override
    public void run(String... args) throws Exception {
        JavaMigration[] javaMigrations = applicationContext.getBeansOfType(JavaMigration.class).values()
                .toArray(new JavaMigration[0]);
        Flyway flyway = Flyway.configure().dataSource(dataSource).javaMigrations(javaMigrations).load();
        flyway.migrate();
    }
}
